package com.px.chapter_Dec_1.russianGame;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;

public class GameContainer extends Observable {
    public static final int ROWS=30;
    public static final int COLS=20;
    public static final int SPEED=500;

    private List<int[]> data;
    private Shape shape;

    public GameContainer(){
        data=new ArrayList<>();
        for (int i=0;i<ROWS;i++)
            data.add(new int[COLS]);
    }
    public void start(){
        shape=ShapeFactory.getShape();
        setChanged();
        notifyObservers(shape);
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (canDown()){
                    shape.down();
                    setChanged();
                    notifyObservers(shape);
                    try {
                        Thread.sleep(SPEED);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                start();
            }
        }).start();
    }
    private boolean canDown(){
        int top=shape.getTop();
        int[][] data=shape.getData();
        for (int i=data.length-1;i>=0;i--)
            for(int j=0;j<data[i].length;j++){
                System.out.println(top+"  "+i+" "+(top+i)+" "+data[i][j]);//调试
                if(data[i][j]==1&&top+i>26){
                    return false;
                }
            }
        return true;
    }
}
